fid = fopen('e:\manoj\temp\piu.txt','rt');
[A, count] = fscanf(fid,'%s',[5,inf]);
fclose all;
A = A';
st = 0;
en = 0;
nday = 0;
for i = 1:length(A),
    S = dir(['e:\manoj\geomag\data\PIU1\' A(i,:)]);
    if length(S) > 2,
        for j = 1:length(S)-2,
            if S(j+2).name(end) == 'M' & S(j+2).isdir == 0,
                fid = fopen(['e:\manoj\geomag\data\PIU1\' A(i,:) '\' S(j+2).name],'rt');
                if fid ~= -1,
                    fgetl(fid);fgetl(fid);fgetl(fid);fgetl(fid);
                    [Data,count] = fscanf(fid,'%f',[10,inf]);
                    fclose(fid);
                    if count > 10,
                        fprintf('%s\n',['c:\manoj\geomag\data\PIU1\' A(i,:) '\' S(j+2).name]);
                    Data = Data';
                    st = en + 1;
                    en = st+size(Data,1)-1;
                    
                    fday(st:en) = datenum(Data(:,3),Data(:,2),Data(:,1),Data(:,4),Data(:,5),30)-730486;
                    H_field(st:en) = Data(:,7);
					nday = nday+1;
                    N_data(nday) = size(Data,1);
                    U_fday(nday) = floor(fday(st));
                    end;
                end;
            end;
        end;
    end;
end;
% [n_fday,b] = sort(fday);
% n_H_field = H_field(b);
% %N_data = N_data(b);
% [U_fday,bb] = sort(U_fday);
% N_data = N_data(bb);             

H_daily = ones([1742,1440])*NaN;
st = 0;
en = 0;
for i = 1:1742,
st = en + 1;
en = st + N_data(i)-1;
[HH,l] = unique(n_fday(st:en));
H_data = n_H_field(st:en);
H_data = H_data(l);

[trash,l] = intersect(aa,HH-floor(HH));

H_daily(i,l) = H_data;
% plot(n_fday(st:en),n_H_field(st:en));
% title(sprintf('%d',U_fday(i)));
% pause;
end;



S = dir('E:\manoj\geomag\data\HUA\');
st = 0;
en = 0;

for i = 1:length(S)-2,
    
    
    [DataX,err_x] = obs_read(['e:\manoj\geomag\data\HUA\' S(i+2).name],'X');
    [DataY,err_y] = obs_read(['e:\manoj\geomag\data\HUA\' S(i+2).name],'Y');
%     fprintf('%s\n',S(i+2).name);
    
    if size(DataX,1) > 1 & err_x ~= 1 size(DataY,1) > 1 & err_y ~= 1,
        fprintf('XY %s\n',S(i+2).name);
        st = en+1;
        en = st + size(DataX,1)-1;
        L = DataX == 99999 | DataY == 99999;
        DataX(L) = NaN;
        DataY(L) = NaN;
        H_field(st:en,:) = sqrt(DataX(:,5:end).^2+DataY(:,5:end).^2);
        h_fday(st:en,:) = DataX(:,1:4);
%         X_field(st_x:en_x,:) = DataX;
%         st_y = en_y+1;
%         en_y = st_y + size(DataY,1)-1;
%         Y_field(st_y:en_y,:) = DataY;
        
    else,
        [DataH,err_h] = obs_read(['e:\manoj\geomag\data\HUA\' S(i+2).name],'H');
        if size(DataH,1) > 1 & err_h ~= 1,
        fprintf('H %s\n',S(i+2).name);
        st = en+1;
        en = st + size(DataH,1)-1;
        H_field(st:en,:) = DataH(:,5:end);
        h_fday(st:en,:) = DataH(:,1:4);
        L = DataH == 99999;
        DataH(L) = NaN;
        else,
            fprintf('No data %s\n',S(i+2).name);
        end;
        
    end;
    if max(max(H_field)) > 1e5,
            return;
        end;
    
end;
       
reshape(H_field',[24*60,1827]);
[C,IA,IB] = intersect(U_fday,HUA_fday);

for i = 1000:1100,
    plot(HUA_daily(IB(i),:)-nanmean(HUA_daily(IB(i),200:600)));
    hold on;
    plot(H_daily(IA(i),:)-nanmean(H_daily(IA(i),200:600)),'r');
    pause;
    hold off;
end;



HUA_H_common = HUA_daily(IB,:)- repmat(nanmean(HUA_daily(IB,200:500),2),[1,1440]);
PIU_H_common = H_daily(IA,:) - repmat(nanmean(H_daily(IA,200:500),2),[1,1440]);

xaxis = 1:1440;
i = 1;
for kk = 1:142,
for jj = 1:15,
    subplot(5,3,jj);
    hold off;
  L = ~(isnan(HUA_H_common(i,:)) |isnan(PIU_H_common(i,:)));
  k = find(L==1);
    if sum(L) > 100,
      [p1] = polyfit(xaxis(L),HUA_H_common(i,L),20);
      [p2] = polyfit(xaxis(L),PIU_H_common(i,L),20);
      [y1] = polyval(p1,xaxis(k(1):k(end)));
      [y2] = polyval(p2,xaxis(k(1):k(end)));
      
      plot(xaxis,HUA_H_common(i,:),'r-',xaxis(k(1):k(end)),y1,'g-');
      hold on;
      plot(xaxis,PIU_H_common(i,:),'k-',xaxis(k(1):k(end)),y2,'c-');
      title(sprintf('%d',i));
    end;
      i=i+1;
end;
pause;
 
end;

load c:\manoj\projects\plasma\Julia_PLP.mat
load c:\manoj\projects\plasma\HUA_PIU.mat
pp=1;
fday_list_julia = floor(fday(:,1));
fday_julia_frac = fday(1,:) - floor(fday(1,:));
[C,IA,IB] = intersect(fday_list_julia,HUA_PIU_fday);
for i = 1:100,
 for jj = 1:10,
 subplot(5,2,jj);
 [AX,H1,H2] = plotyy(magnetic_frac,HUA_H_common(IB(pp),:)-PIU_H_common(IB(pp),:),fday_julia_frac,w(IA(pp),:));
 set(H1,'Color','r');
 set(H2,'Color','b');
 set(get(AX(1),'Ylabel'),'String','HUA-PIU (nT)');
 set(get(AX(2),'Ylabel'),'String','Julia Drift (m/s)');
 set(get(AX(1),'Ylabel'),'color','r');
 set(get(AX(2),'Ylabel'),'color','b');

 title(sprintf('mjd = %d',C(pp)));
 pp=pp+1;
 end;
 pause;
end;

S = dir('c:\manoj\geomag\data\HUA\hua2005*.min');

for i = 1:365,
fid = fopen(['c:\manoj\geomag\data\HUA\' S(i).name],'rt');
for k = 1:23,
fgetl(fid);
end;
for j = 1:1440,
    P = fgetl(fid);
    HUA_2005(i,j) = sscanf(P(33:43),'%f',1);
end;
fclose(fid);
end;
load c:\manoj\projects\plasma\HUA_daily.mat

HUA_daily = ([HUA_daily; HUA_2005]);
HUA_fday(end+1:end+365) = (datenum(2005,1,1) - 730486):(datenum(2005,12,31) - 730486);
load c:\manoj\projects\plasma\PIU_H.mat H_daily U_fday;
[C,IA,IB] = intersect(U_fday,HUA_fday);
HUA_H_common = HUA_daily(IB,:)- repmat(nanmean(HUA_daily(IB,200:500),2),[1,1440]);
PIU_H_common = H_daily(IA,:) - repmat(nanmean(H_daily(IA,200:500),2),[1,1440]);

load c:\manoj\projects\plasma\HUA_PIU1
% fid = fopen('c:\manoj\temp\hua_piu.txt','wt');
count = 0;
for i = 1:1739,
    L = isnan(HUAPIU(i,:));
    if sum(L) < 100,
        count = count+1;
%         fprintf(fid,'%4d ',HUA_PIU_fday(i));
%         fprintf(fid,'%4.0f ',HUAPIU(i,:));
%         fprintf(fid,'\n');
    end;
end;
fclose all
    
